﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using BotvaBot.Configuration.Personaj;


namespace BotvaBot.Configuration
{
    public class CustomConfig : ConfigurationSection
    {
        [ConfigurationProperty("PersonajSet")]
        public GenericConfigElementCollection<PersonajCfg> PersonajSet
        {
            get
            {
                return base["PersonajSet"]
                    as GenericConfigElementCollection<PersonajCfg>;
            }
        }

        public int MonsterFightDelitel
        {
            get { return ReadIntFlag("MonsterFightDelitel", 3); }
        }

        public int MonsterFightLevel
        {
            get { return ReadIntFlag("MonsterFightLevel", 1); }
        }


        public static bool ReadBooleanFlag(string flagName, bool defaultValue)
        {
            bool result = defaultValue;
                
            if (!ConfigurationManager.AppSettings.AllKeys.Contains(flagName))
                return result;
            
            string strValue = ConfigurationManager.AppSettings[flagName];
                
            if (strValue == null || strValue.Trim() == string.Empty)
                return result;

            if (strValue.ToLower().Equals("true") || strValue.ToLower().Equals("t"))
                return true;

            if (strValue.ToLower().Equals("false") || strValue.ToLower().Equals("f"))
                return false;

            //int iValue = Parsers.GetInt32(strValue, 0);
            int iValue = 0;
            if(Int32.TryParse(strValue, out iValue))
            {
                result = iValue == 1;
            }
            else
            {
                result = defaultValue;
            }

            return result;
        }

        public static int ReadIntFlag(string flagName, int defaultValue)
        {
            int result = defaultValue;

            if (!ConfigurationManager.AppSettings.AllKeys.Contains(flagName))
                return result;

            string strValue = ConfigurationManager.AppSettings[flagName];

            if (strValue == null || strValue.Trim() == string.Empty)
                return result;

            if(!int.TryParse(strValue, out result))
            {
                result = defaultValue;
            }

            return result;
        }
    }
}
